<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="10" :lg="11" :md="12" :sm="24">
            <a-form-item label="创建日期">
              <j-date :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择开始时间" class="query-group-cust" v-model="queryParam.createTime_begin"></j-date>
              <span class="query-group-split-cust"></span>
              <j-date :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择结束时间" class="query-group-cust" v-model="queryParam.createTime_end"></j-date>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="ID">
              <a-input placeholder="请输入ID" v-model="queryParam.userId"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="用户昵称">
                <a-input placeholder="请输入用户昵称" v-model="queryParam.userName"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="提现方式">
                <j-dict-select-tag placeholder="请选择提现方式" v-model="queryParam.payType" dictCode="withdrawal_type"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="提现账户">
                <a-input placeholder="请输入提现账户" v-model="queryParam.account"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="提现状态">
                <j-dict-select-tag placeholder="请选择提现状态" v-model="queryParam.status" dictCode="withdrawal_status"/>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
<!--      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>-->
      <a-button type="primary" icon="download" @click="handleExportXls('用户提现记录')">导出</a-button>
      <a-button type="primary" @click="batchOnePayment">一键手动打款</a-button>
      <a-button type="primary" @click="batchSubmitTask">一键线上打款</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="statusSlot" slot-scope="text, record">
          <div v-if="record.status === '1'" style="color: #169bd5">{{text}}</div>
          <a v-else-if="record.status === '2'" @click="openPaymentInfo(record)" style="color: #ff962f">{{text}}</a>
          <a v-else-if="record.status === '3'" @click="openPaymentInfo(record)" style="color: #000000">{{text}}</a>
          <a v-else-if="record.status === '4'" @click="openPaymentInfo(record)" style="color: red">{{text}}</a>
          <a v-else @click="openPaymentInfo(record)" style="color: #152ede">{{text}}</a>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="auditInfo(record,1)" v-if="record.status === '1'">审核通过</a>
          <a-divider type="vertical" v-if="record.status === '1'"/>
          <a @click="auditInfo(record,2)" v-if="record.status === '1'">审核拒绝</a>
          <a-divider type="vertical" v-if="record.status === '1'"/>
          <a-popconfirm title="确定已打款吗?" v-if="record.status === '2'" @confirm="() => onePayment(record.id)">
            <a>一键手动打款</a>
          </a-popconfirm>
          <a-divider type="vertical" v-if="record.status === '2'"/>
          <a-popconfirm title="确定线上打款吗?" v-if="record.status === '2'" @confirm="() => submitTask(record.id)">
            <a>一键线上打款</a>
          </a-popconfirm>
        </span>

      </a-table>
    </div>
    <a-modal v-model="visible" :width="width" :title="title" @ok="audit" @cancel="close">
      <a-descriptions>
        <a-descriptions-item label="提现方式">
          {{clickRecord.payType_dictText}}
        </a-descriptions-item>
      </a-descriptions>
      <a-descriptions title="身份信息">
        <a-descriptions-item label="姓名">
          {{clickRecord.realName}}
        </a-descriptions-item>
        <a-descriptions-item label="提现账户">
          <span v-if="clickRecord.payType=='3'">{{clickRecord.account}}({{clickRecord.bankName}})</span>
          <span v-else>{{clickRecord.account}}</span>
        </a-descriptions-item>
      </a-descriptions>
      <a-descriptions title="提现信息">
        <a-descriptions-item label="提现金额">
          {{clickRecord.withdrawalAmount}} 元
        </a-descriptions-item>
        <a-descriptions-item label="手续费">
          {{clickRecord.commission}} 元
        </a-descriptions-item>
        <a-descriptions-item label="到账金额">
          {{clickRecord.receivedAmount}} 元
        </a-descriptions-item>
        <a-descriptions-item :span="3" label="提现描述">
          <a-input style="width: 1000px" v-model="withdrawalDetail" placeholder="提现描述"></a-input>
        </a-descriptions-item>
      </a-descriptions>
    </a-modal>
    <Payment-Info-Item-List ref="PaymentInfoItemList"></Payment-Info-Item-List>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import { postAction,getAction } from '@/api/manage'
  import {filterMultiDictText} from '@/components/dict/JDictSelectUtil'
  import PaymentInfoItemList from './modules/PaymentInfoItemList'

  export default {
    name: 'WithdrawalList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      PaymentInfoItemList
    },
    data () {
      return {
        description: '用户提现记录管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'ID',
            align:"center",
            dataIndex: 'userId'
          },
          {
            title:'昵称',
            align:"center",
            dataIndex: 'userName'
          },
          {
            title:'提现方式',
            align:"center",
            dataIndex: 'payType_dictText'
          },
          {
            title:'提现用户名',
            align:"center",
            dataIndex: 'realName'
          },
          {
            title:'提现账户',
            align:"center",
            dataIndex: 'account',
            customRender: function(text,record) {
              if(record.payType=='3'){
                return record.bankName+"("+text+")";
              }else{
                return text;
              }
            }
          },
          {
            title:'提现金额(元)',
            align:"center",
            dataIndex: 'withdrawalAmount'
          },
          {
            title:'兑换钻石',
            align:"center",
            dataIndex: 'diamondNum'
          },
          {
            title:'到账金额(元)',
            align:"center",
            dataIndex: 'receivedAmount'
          },
          {
            title:'手续费(元)',
            align:"center",
            dataIndex: 'commission'
          },
          {
            title:'申请时间',
            align:"center",
            dataIndex: 'createTime'
          },
          {
            title:'提现状态',
            align:"center",
            dataIndex: 'status_dictText',
            scopedSlots: { customRender: 'statusSlot' }
          },
          {
            title:'审核时间',
            align:"center",
            dataIndex: 'auditTime'
          },
          {
            title:'提现描述',
            align:"center",
            dataIndex: 'withdrawalDetail'
          },
          {
            title:'打款时间',
            align:"center",
            dataIndex: 'paymentTime'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/user/withdrawal/list",
          delete: "/user/withdrawal/delete",
          deleteBatch: "/user/withdrawal/deleteBatch",
          exportXlsUrl: "/user/withdrawal/exportXls",
          importExcelUrl: "user/withdrawal/importExcel",

        },
        dictOptions:{},
        superFieldList:[],
        visible:false,
        title:"",
        width:1200,
        clickRecord:{},
        withdrawalDetail:"",
        status:"",
      }
    },
    created() {
    this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      initDictConfig(){
      },

      auditInfo(record,type){
        this.visible=true;
        if(type==1){
          this.title="审核通过";
          this.clickRecord=record;
          this.status=2;
        }else{
          this.title="审核拒绝";
          this.clickRecord=record;
          this.status=4;
        }
      },
      audit(){
        if(this.status==4 && !this.withdrawalDetail){
          this.$message.warning("请填写提现描述");
          return;
        }
        this.loading=true;
        var obj={};
        obj.id=this.clickRecord.id;
        obj.status=this.status;
        obj.withdrawalDetail=this.withdrawalDetail;
        postAction('/user/withdrawal/auditPass', obj).then(({success, result, message}) => {
          if (success) {
            this.$message.success(message)
            this.loadData();
            this.close();
          } else {
            this.$message.warning(message)
          }
        }).finally(() => this.loading = false)
      },

      //一键线下打款
      onePayment(id){
        this.loading=true;
        getAction('/user/withdrawal/onePayment', {id:id}).then(({success, result, message}) => {
          if (success) {
            this.$message.success(message)
            this.loadData();
          } else {
            this.$message.warning(message)
          }
        }).finally(() => this.loading = false)
      },

      //一键线上打款
      submitTask(id){
        this.loading=true;
        getAction('/payment/paymentInfo/submitTask', {ids:id}).then(({success, result, message}) => {
          if (success) {
            this.$message.success(message)
            this.loadData();
          } else {
            this.$message.warning(message)
          }
        }).finally(() => this.loading = false)
      },

      //批量一键打款
      batchOnePayment(id){
        if(this.selectedRowKeys.length==0){
          this.$message.warning("请至少选择一条记录进行打款");
          return;
        }
        var ids = this.selectedRowKeys.join(",");
        for (let i = 0; i < this.selectionRows.length; i++) {
          if(this.selectionRows[i].status!=2){
            this.$message.warning("存在不是待打款状态的记录");
            return;
          }
        }

        this.loading=true;
        getAction('/user/withdrawal/batchOnePayment', {ids:ids}).then(({success, result, message}) => {
          if (success) {
            this.$message.success(message)
            this.loadData();
          } else {
            this.$message.warning(message)
          }
        }).finally(() => this.loading = false)
      },


      //批量一键线上打款
      batchSubmitTask(){
        if(this.selectedRowKeys.length==0){
          this.$message.warning("请至少选择一条记录进行打款");
          return;
        }
        var ids = this.selectedRowKeys.join(",");
        for (let i = 0; i < this.selectionRows.length; i++) {
          if(this.selectionRows[i].status!=2){
            this.$message.warning("存在不是待打款状态的记录");
            return;
          }
        }

        this.submitTask(ids);
      },

      close(){
        this.clickRecord={};
        this.title="";
        this.withdrawalDetail="";
        this.status="",
        this.visible=false;
      },

      openPaymentInfo(record){
        this.$refs.PaymentInfoItemList.initData(record.id);
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>